package com.guit.hrms.controller;

import com.guit.hrms.dto.interview.InterviewDTO;
import com.guit.hrms.dto.interview.InterviewSituationDTO;
import com.guit.hrms.dto.interview.UpdateInterviewDTO;
import com.guit.hrms.po.utilsPO.Result;
import com.guit.hrms.service.InterviewService;
import com.guit.hrms.utils.ThreadLocalUtil;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@CrossOrigin
@RestController
@RequestMapping("/api/interview")
public class InterviewController {
    @Autowired
    private InterviewService interviewService;

    /**
     * 添加面试安排
     * @param interviewDTO 面试安排信息
     * @return Result
     */
    @PostMapping
    public Result addInterview(@Validated @RequestBody InterviewDTO interviewDTO){
        if (interviewDTO.getStartTime().isAfter(interviewDTO.getEndTime())){
            return Result.error("开始时间不能晚于结束时间");
        }
        return interviewService.addInterview(interviewDTO) == 1 ? Result.success() : Result.error("信息有误或时间地点冲突");
    }

    /**
     * 获取个人面试安排
     * @param page 页码
     * @param pageSize 页大小
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return Result
     */
    @GetMapping
    public Result getPersonalInterviews(@RequestParam(defaultValue = "1") Integer page ,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                LocalDateTime startTime, LocalDateTime endTime){
        Map<String,Object> map = ThreadLocalUtil.get();
        String employeeId = map.get("employeeId").toString();
        return Result.success(interviewService.getPersonalInterviews(page,pageSize,startTime,endTime,employeeId));
    }

    /**
     * 获取所有面试安排
     * @param page 页码
     * @param pageSize 页大小
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return Result
     */
    @GetMapping("/all")
    public Result getAllInterviews(@RequestParam(defaultValue = "1") Integer page ,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        LocalDateTime startTime, LocalDateTime endTime){
        return Result.success(interviewService.getAllInterviews(page,pageSize,startTime,endTime));
    }

    /**
     * 修改面试安排
     * @param updateInterviewDTO 面试安排信息
     * @return Result
     */
    @PatchMapping
    public Result updateInterview(@Validated @RequestBody UpdateInterviewDTO updateInterviewDTO){
        if (updateInterviewDTO.getInterviewerIds() == null
                && updateInterviewDTO.getInterviewRound() == null
                && updateInterviewDTO.getName() == null
                && updateInterviewDTO.getPlace() == null
                && updateInterviewDTO.getRecruitsNumber() == null
                && updateInterviewDTO.getStartTime() == null
                && updateInterviewDTO.getEndTime() == null){
            return Result.error("请输入修改的信息！");
        } else {
            return interviewService.updateInterview(updateInterviewDTO) == 1 ? Result.success() : Result.error("信息有误或时间地点冲突");
        }
    }

    /**
     * 删除面试安排
     * @param interviewIds 面试安排id
     * @return Result
     */
    @DeleteMapping("/{interviewIds}")
    public Result deleteInterview(@PathVariable List<Integer> interviewIds){
        return interviewService.deleteInterview(interviewIds) != 0 ? Result.success() : Result.error("删除失败,请确定信息正确！");
    }

    /**
     * 开始面试
     * @param interviewId 面试安排id
     * @param startOrEnd 1开始面试 2结束面试
     * @return Result
     */
    @PostMapping("/start")
    public Result interviewStartOrEnd(@RequestParam @NotNull(message = "interviewId不能为空") Integer interviewId,@RequestParam @NotNull(message = "startOrEnd不能为空") Integer startOrEnd){
        System.out.println("调用面试开始/结束接口，实际状态:" + (startOrEnd == 1 ? "开始" : "结束")  );
        System.out.println("接收到的参数: interviewId=" + interviewId + ", startOrEnd=" + startOrEnd);
        return interviewService.interviewStartOrEnd(interviewId, startOrEnd) == 1 ? Result.success() : Result.error("面试开始失败");
    }


    /**
     * 获取面试详情
     * @param page 页码
     * @param pageSize 页大小
     * @param interviewId 面试id
     * @param intervieweeId 应聘者id
     * @return Result
     */
    @GetMapping("/situation")
    public Result getInterviewSituation(@RequestParam(defaultValue = "1") Integer page ,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   Integer interviewId, Integer intervieweeId){
        return Result.success(interviewService.getInterviewSituation(page,pageSize,interviewId,intervieweeId));
    }

    /**
     * 更新面试详情（为应聘者评分）
     * @param interviewSituationDTO 面试详情信息
     * @return Result
     */
    @PatchMapping("/situation")
    public Result updateInterviewSituation(@Validated @RequestBody InterviewSituationDTO interviewSituationDTO){
        return Result.success(interviewService.updateInterviewSituation(interviewSituationDTO) == 1 ? "更新成功" : "更新失败");
    }

}
